package com.ebt.m.customer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.entity.RiskBean;

import java.util.List;

/**
 * 选择主险
 * Created by archer.qi on 2016/12/8.
 */
public class CheckRiskAdapter extends BaseAdapter {

    private Context context;
    private List<RiskBean> data;
    private LayoutInflater inflater;
    private int checkedPosition = -1;
    private String checkedId;

    public CheckRiskAdapter(Context context, List<RiskBean> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    public void setCheckedPosition(int pos) {
        checkedPosition = pos;
        notifyDataSetChanged();
    }

    public void setCheckId(String id) {
        checkedId = id;
    }

    /**
     * 获取选中
     *
     * @return
     */
    public RiskBean getItemSelected() {
        if (data != null && data.size() > 0) {
            if (checkedPosition >= 0 && checkedPosition < data.size()) {
                return data.get(checkedPosition);
            }
        }
        return null;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_check_risk, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.setText(data.get(i).getShortName());

        if (checkedPosition == -1) {
            // 初始判断时，checkPosition = -1，以id为判断是否选中的标准
            if (data.get(i).getId().equals(checkedId)) {
                checkedPosition = i;
                holder.setChecked(true);
            } else {
                holder.setChecked(false);
            }
        } else {
            // 非初始判断时，以checkPosition为判断是否选中的标准
            if (checkedPosition == i) {
                holder.setChecked(true);
            } else {
                holder.setChecked(false);
            }
        }

        return view;
    }


    private class ViewHolder {
        private ImageView mIvStatus;
        private TextView mTvTitle;

        public ViewHolder(View view) {
            mTvTitle = (TextView) view.findViewById(R.id.item_check_risk_title);
            mIvStatus = (ImageView) view.findViewById(R.id.item_check_risk_status);
        }

        public void setText(String text) {
            mTvTitle.setText(text == null ? "" : text);
        }

        public void setChecked(boolean checked) {
            mIvStatus.setImageResource(checked ? R.drawable.ic_item_insurance_selected : R.drawable.ic_item_insurance_unselected);
        }
    }
}
